package ui.activity.poscontrol;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.poscontrol.adapter.HasPushPosRecordAdapter;
import ui.activity.poscontrol.beanmodel.PosMainBean;
import ui.activity.poscontrol.beanmodel.PushPosBean;
import ui.activity.poscontrol.biz.PosControlMainBiz;
import ui.activity.poscontrol.component.DaggerPosControlMainComponent;
import ui.activity.poscontrol.contract.PosControlMainContract;
import ui.activity.poscontrol.module.PosControlMainModule;
import ui.activity.poscontrol.presenter.PosControlMainPresenter;


/**
 * 已划拨终端列表
 * Created by chenjian on 18/2/22.
 */

public class HasPushPosAct extends BaseAct implements PosControlMainContract.View {

    @Inject
    PosControlMainPresenter presenter;
    @Inject
    PosControlMainBiz biz;


    @BindView(R.id.pos_num_text)
    TextView pos_num_text;//设备数量
    @BindView(R.id.hasback_text)
    TextView hasback_text;//开通数量
    @BindView(R.id.flow_custom_name)
    TextView flow_custom_name;//开通数量
    @BindView(R.id.peoplehasposnum)
    TextView peoplehasposnum;//开通数量


    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    private int page = 1;
    private int size = 100;
    private int toId;
    private int count;
    private String name;
    private List<PushPosBean.RecordsBean> userInfoData = new ArrayList<>();
    HasPushPosRecordAdapter adapter;
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_haspushpos);
    }

    @Override
    protected void setup() {
        DaggerPosControlMainComponent.builder().posControlMainModule(new PosControlMainModule(this)).build().inject(this);
        setToolBar("已划拨终端详情", "");
        presenter.setBiz(biz);
        toId =getIntent().getIntExtra("toid",0);
        count =getIntent().getIntExtra("count",0);
        name = getIntent().getStringExtra("name");
        pos_num_text.setText("终端数量:"+count+"台");
        flow_custom_name.setText(name);
        peoplehasposnum.setText(count+"台");
        rv.setLayoutManager(new LinearLayoutManager(HasPushPosAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new DividerItemDecoration(HasPushPosAct.this,1));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);

        rv.setOnLoadMoreListener(() -> {
            getData(false);
        });

        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData(true);
            }
        });
        ptr.autoRefresh();
    }

    private void getData(boolean refresh){
        if(refresh){
            page = 1;
            userInfoData.clear();
        }else{
            page++;
        }

        biz.queryHasPushPosData(TobuyApplication.getCustomId(),toId,page,size,new BaseBiz.Callback<PushPosBean>() {
            @Override
            public void onSuccess(PushPosBean pushPosBean) {
                userInfoData.addAll(pushPosBean.getRecords());
                if(null == adapter){
                    adapter = new HasPushPosRecordAdapter(HasPushPosAct.this,userInfoData);
                    rv.setAdapter(adapter);
                }else{
                    adapter.setupData(userInfoData);
                }
                rv.setHasLoadMore(pushPosBean.getRecords().size()==size);
                if(refresh){
                    ptr.onRefreshComplete();
                }else {
                    rv.onLoadMoreComplete();
                }

            }

            @Override
            public void onFailure(PushPosBean pushPosBean) {

            }

        });
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }




    @Override
    public void upDateUI(PosMainBean bean) {
//        my_pos_num.setText(bean.getPosCount()+"台");
//        posrecord_txt_total.setText("终端数量："+(bean.getPosCount()+bean.getFlowCount())+"台");
//        posopen_txt_total.setText("已开通："+bean.getOpenedCount()+"台");
//        posrecord_txt_jh.setText("已激活"+bean.getActiveCount()+"台");
//        push_pos_num.setText(bean.getFlowCount()+"台");
    }

    @Override
    public void showPopWindow() {
    }

    @Override
    public void showFlowRuleDialog(String items) {
    }
}
